package com.gbq.service.impl;


import com.gbq.dao.DeviceDao;
import com.gbq.dao.KeyPersonDao;
import com.gbq.service.BaseService;
import com.gbq.vo.Device;
import com.gbq.vo.KeyPerson;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BaseServiceImpl implements BaseService{


    @Autowired
    private DeviceDao deviceDao;

    @Autowired
    private KeyPersonDao keyPersonDao;

    @Autowired
    RedisTemplate redisTemplate;


    @Override
    public String selectBySn(String sn) {
        return deviceDao.selectBySn(sn);
    }

    @Override
    public List<KeyPerson> selectByCardNo(String idCardNo) {
        return keyPersonDao.selectByCardNo(idCardNo);
    }

    @Override
    public void updateDeviceStatus(String validity, String sn, Date updateTime) {
        deviceDao.updateDeviceStatus(validity,sn,updateTime);
    }

    @Override
    public List<Device> select() {
        return deviceDao.select();
    }

    @Override
    public JSONObject selectPersonByCardno(String cardno) {
        RedisSerializer<String> serializer = new StringRedisSerializer();
        ValueOperations<String,Object> valueOperations = redisTemplate.opsForValue();
        redisTemplate.setKeySerializer(serializer);
        redisTemplate.setValueSerializer(serializer);

        JSONObject obj = JSONObject.fromObject(valueOperations.get("T_PERSON_ON_BLACK_LIST"+cardno));

        return obj ;
    }


}
